home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Car / Transmission.m < prev    next >
Encoding:
Text File  |  1992-06-17  |  3.3 KB  |  202 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Car_main.h"
  5. #import "Transmission.h"
  6. #import "Wheels.h"
  7. #import "Cycle.h"
  8. #import "defs.h"
  9.  
  10. @implementation Transmission
  11.  
  12. - init
  13. {
  14.     [super init];
  15.     transmission = self;
  16.     return self;
  17. }
  18.  
  19. - read:(NXTypedStream *)stream
  20. {
  21.     [super read:stream];
  22.     NXReadTypes(stream,"iffff",¤tGear,&efficiency,&finalDriveRatio,&mass,&shiftLag);
  23.     NXReadArray(stream,"f",5,gearRatios);
  24.     NXReadArray(stream,"f",4,shiftPoints);
  25.     return self;
  26. }
  27.  
  28. - write:(NXTypedStream *)stream
  29. {
  30.     [super write:stream];
  31.     NXWriteTypes(stream,"iffff",¤tGear,&efficiency,&finalDriveRatio,&mass,&shiftLag);
  32.     NXWriteArray(stream,"f",5,gearRatios);
  33.     NXWriteArray(stream,"f",4,shiftPoints);
  34.     return self;
  35. }
  36.  
  37. - (int)currentGear
  38. {
  39.     return currentGear;
  40. }
  41.  
  42. - setCurrentGear:(int)aNumber
  43. {
  44.     currentGear = aNumber;
  45.     return self;
  46. }
  47.  
  48. - (float)efficiency
  49. {
  50.     return efficiency;
  51. }
  52.  
  53. - setEfficiency:(float)aNumber
  54. {
  55.     efficiency = aNumber;
  56.     return self;
  57. }
  58.  
  59. - (float)finalDriveRatio
  60. {
  61.     return finalDriveRatio;
  62. }
  63.  
  64. - setFinalDriveRatio:(float)aNumber
  65. {
  66.     finalDriveRatio = aNumber;
  67.     return self;
  68. }
  69.  
  70. - (float *)gearRatios
  71. {
  72.     return gearRatios;
  73. }
  74.  
  75. - setGearRatios:(float *)theRatios
  76. {
  77. int i;
  78.  
  79.     for ( i = 0 ; i < 5 ; i++ )
  80.         gearRatios[i] = theRatios[i];
  81.     return self;
  82. }
  83.  
  84. - (float)mass
  85. {
  86.     return mass;
  87. }
  88.  
  89. - setMass:(float)aNumber
  90. {
  91.     mass = aNumber;
  92.     return self;
  93. }
  94.  
  95. - (float)shiftLag
  96. {
  97.     return shiftLag;
  98. }
  99.  
  100. - setShiftLag:(float)aNumber
  101. {
  102.     shiftLag = aNumber;
  103.     return self;
  104. }
  105.  
  106. - (float*)shiftPoints
  107. {
  108.     return shiftPoints;
  109. }
  110.  
  111. - setShiftPoints:(float *)thePoints
  112. {
  113. int i;
  114.  
  115.     for ( i = 0 ; i < 4 ; i++ )
  116.         shiftPoints[i] = thePoints[i];
  117.     return self;
  118. }
  119.  
  120. - (float)inputSpeed
  121. {
  122. float outputSpeed;
  123. float inputSpeed;
  124. int tempGear;
  125. float rpm;
  126.  
  127.     if ( shifting )
  128.         return 0;
  129.  
  130.     outputSpeed = [wheel speed];
  131.     outputSpeed = outputSpeed * finalDriveRatio;
  132.         
  133.     for ( tempGear = 1 ; tempGear < 5 ; tempGear++ )        // Favors the lowest gear possible to do the job.
  134.         {
  135.         inputSpeed = outputSpeed * gearRatios[tempGear-1];
  136.         rpm = inputSpeed * 60 / ( 2 * PI );
  137.         if ( rpm < shiftPoints[tempGear-1] )
  138.             break;
  139.         }
  140.     if ( rpm > shiftPoints[tempGear-1] )
  141.         tempGear = 5;
  142.  
  143.     // Now that we've found the proper gear, we have to decide if we want to shift to it.
  144.     if ( tempGear == currentGear - 1 )        // We have a 500 rpm buffer zone where the Transmission won't downshift.
  145.         if ( rpm > shiftPoints[tempGear-1] - 500 )
  146.             tempGear = currentGear;
  147.  
  148.     if ( currentGear != tempGear )
  149.         {
  150.         currentGear = tempGear;
  151.         shifting = YES;
  152.         shiftTime = [cycle lastTime];
  153.         }
  154.     inputSpeed = outputSpeed * gearRatios[currentGear-1]; 
  155.     return inputSpeed;
  156. }
  157.  
  158. - engineInput:(float)torque
  159. {
  160.     engineTorque = torque;
  161.     return self;
  162. }
  163.  
  164. - motorInput:(float)torque
  165. {
  166. float total;
  167.  
  168.     motorTorque = torque;
  169.     total = motorTorque + engineTorque;
  170.     total *= gearRatios[currentGear - 1];
  171.     total *= finalDriveRatio;
  172.  
  173.     if ( shifting )
  174.         {
  175.         if ( [cycle lastTime] - shiftTime < shiftLag )
  176.             total = 0;
  177.         else
  178.             shifting = NO;
  179.         }
  180.  
  181.     total = total * efficiency;
  182.     [wheel inputTorque:total];
  183.     return self;
  184. }
  185.  
  186. @end
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.